package com.china08.yxyapi.api.home.activity.marker;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.maker.YxyMakerCatalog;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.marker.YxyMakerCatalogRepository;

/**
 * 区县教育局-竞赛报名-项目名下拉框展示
 * 
 * @author zd.Jin
 *
 *
 */
@RestController
@CrossOrigin
public class YxyMakerCatalogLstBySelectController extends YxyApiBaseController {
    @Autowired
    private YxyMakerCatalogRepository yxyMakerCatalogRepository;

    @RequestMapping(value = "/api_yw/home/activity/competition/catalog/list/bypid", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody ReqModel reqModel) {
        List<RespModel> respModels = new ArrayList<>();
        RespModel res = new RespModel();
        res.id = "all";
        res.name = "全部项目";
        respModels.add(res);
        if (!"all".equals(reqModel.pId)) {
            List<YxyMakerCatalog> list = yxyMakerCatalogRepository.findByPid(reqModel.pId);
            for (YxyMakerCatalog cat : list) {
                RespModel respModel = new RespModel();
                respModel.id = cat.getId();
                respModel.name = cat.getName();
                respModels.add(respModel);
            }
        }
        return respModels;
    }

    protected static class ReqModel extends YxyApiReqModel {

        public String pId;// 创客分类的pId,全部:all

        public void check() {
            Assert.notNull(StringUtils.defaultIfBlank(pId, null), "创客分类的pId不能为空");
        }

    }

    protected static class RespModel extends YxyApiRespModel {
        // 创客分类id
        public String id;
        // 创客分类的名称
        public String name;
    }
}
